package com.example.RuanJianKeShe.Service.login;

import com.alibaba.fastjson.JSONObject;
import com.example.RuanJianKeShe.Bean.Teacher;
import com.example.RuanJianKeShe.Dao.impl.TeacherDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/getTnoByLogin")
public class getTnoByLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        Object tno = session.getAttribute("myTno");
        TeacherDaoImpl teacherDao = new TeacherDaoImpl();
        Teacher teacher = teacherDao.getTeacherById(Integer.parseInt(tno.toString()));
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",teacher.getTname());
        map.put("tno",teacher.getTno());
        String toJSONString = JSONObject.toJSONString(map);
        resp.getWriter().write(toJSONString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
